/**
 * Copyright (c) 2011 Tomi Paananen.
 *
 * Part of Quick Gestures engine library.
 */

#ifndef STORAGETHREAD_H
#define STORAGETHREAD_H

// Qt includes
#include <QThread>

// qgengine includes
#include "qgenginepriv.h"

// Forward declarations
class Gesture;
class QUrl;
class StorageManager;


class QGLIB_EXPORT StorageThread : public QThread
{
    Q_OBJECT
    Q_ENUMS(StorageStates)
    Q_PROPERTY(int state READ state NOTIFY stateChanged)

public: // Data types

    enum StorageStates {
        Idle = 0,
        Loading,
        Saving
    };

public:
    explicit StorageThread(QObject *parent = 0);
    virtual ~StorageThread();

public:
    int state() const;
    int getGestures(QList<Gesture*> &gestureSet);
    StorageManager *storageManager() const;

protected: // From QThread
    void run();

private:
    void setState(StorageStates state);

public slots:
    void load(const QString &filepath);
    void loadByUrl(const QUrl &url);
    void save(const QString &filepath, QList<Gesture*> &gestureSet);
    void saveByUrl(const QUrl &url, QList<Gesture*> &gestureSet);
    void cancel();

signals:
    void stateChanged(int state);
    void finished();
    void failed(int error);
    void cancelled();
    void progress(int percentsComplete);
    void gestureLoaded(Gesture *gesture);

private: // Data
    StorageManager *m_manager; // Owned
    QList<Gesture*> m_gestureSet; // Content not owned
    QString m_filepath;
    StorageStates m_state;
    bool m_doCancel;
};

#endif // STORAGETHREAD_H

// End of file.
